/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.confiteriofuentes.modelo;

import com.confiteriafuentes.modelo.distribucion.Camion;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Administrador
 */
public class CamionDAO {


private Model model;
    private PreparedStatement psInsert;


    public CamionDAO() throws SQLException {
        model = Model.getInstance();
    }

    public void insert(Camion t) throws SQLException {
        if (psInsert == null) {
            String sqlInsert = "INSERT INTO camion (placa,modelo,marca) VALUES (?, ?,?)";
            psInsert = (PreparedStatement) model.getCon().prepareStatement(sqlInsert);
        }

       psInsert.setString(1, t.getPlaca());
       psInsert.setString(2, t.getModelo());
       psInsert.setString(3, t.getMarca());



        psInsert.executeUpdate();
    }

        public List<Camion> getAll() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

             public Camion load(ResultSet rs) throws SQLException {

        Camion u = new Camion();
        u.setPlaca(rs.getString(1));
        u.setModelo(rs.getString(2));
        u.setMarca(rs.getString(3));



        return u;
    }



    public Camion find(String placa) throws SQLException {
                  Connection conex=(Connection) model.getCon();

                if (!model.isConnect())
			throw new SQLException("no hay conexcion");
               Camion ca=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{


                    pstmt=(PreparedStatement) conex.prepareStatement("SELECT * FROM camion WHERE placa=?");
			pstmt.setString(1,placa);
                    rs=pstmt.executeQuery();
			if(rs.next()){
                              ca=(Camion)load(rs);

                        }else{

                        }
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return ca;
    }


    public void delete(Camion t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void update(Camion t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}



